# coding: utf-8

{{>partial_header}}

from dataclasses import dataclass
import typing_extensions
import urllib3
{{#if prstv2}}
from pydantic import RootModel
{{/if}}
from {{packageName}}.request_before_hook import request_before_hook
import json
{{#with operation}}
from urllib3._collections import HTTPHeaderDict
{{/with}}

from {{packageName}}.api_response import AsyncGeneratorResponse
from {{packageName}} import api_client, exceptions
{{> model_templates/imports_schema_types }}

{{#each schemaImports}}
{{{.}}}
{{/each}}

{{#each typeImports}}
{{.}}
{{/each}}

{{#if prstv2}}
from ...api_client import Dictionary
{{#each additionalModelImports}}
{{.}}
{{/each}}

{{/if}}
{{#unless isStub}}
from . import path

{{/unless}}
{{#with operation}}
{{#if queryParams}}
{{> endpoint_parameter_schema_and_def xParams=queryParams xParamsName="Query" }}
{{/if}}
{{#if headerParams}}
{{> endpoint_parameter_schema_and_def xParams=headerParams xParamsName="Header" }}
{{/if}}
{{#if pathParams}}
{{> endpoint_parameter_schema_and_def xParams=pathParams xParamsName="Path" }}
{{/if}}
{{#if cookieParams}}
{{> endpoint_parameter_schema_and_def xParams=cookieParams xParamsName="Cookie" }}
{{/if}}
{{#with bodyParam}}
# body param
{{#each content}}
{{#with this.schema}}
{{! If schemaImports is empty, modelsHaveSchemaSuffix is falsey; otherwise, it is truthy }}
{{> model_templates/schema modelsHaveSchemaSuffix=schemaImports}}
{{/with}}
{{/each}}


request_body_{{paramName}} = api_client.RequestBody(
    content={
{{#each content}}
        '{{{@key}}}': api_client.MediaType({{#if this.schema}}
            schema={{this.schema.baseName}}{{/if}}),
{{/each}}
    },
{{#if required}}
    required=True,
{{/if}}
)
{{/with}}
{{#unless isStub}}
{{#each authMethods}}
{{#if @first}}
_auth = [
{{/if}}
    '{{name}}',
{{#if @last}}
]
{{/if}}
{{/each}}
{{#each servers}}
{{#if @first}}
_servers = (
{{/if}}
    {
        'url': "{{{url}}}",
        'description': "{{#unless description}}No description provided{{else}}{{{description}}}{{/unless}}",
        {{#each variables}}
        {{#if @first}}
        'variables': {
        {{/if}}
            '{{{name}}}': {
                'description': "{{#unless description}}No description provided{{else}}{{{description}}}{{/unless}}",
                'default_value': "{{{defaultValue}}}",
                {{#each enumValues}}
                {{#if @first}}
                'enum_values': [
                {{/if}}
                    "{{{.}}}"{{#unless @last}},{{/unless}}
                {{#if @last}}
                ]
                {{/if}}
                {{/each}}
                }{{#unless @last}},{{/unless}}
        {{#if @last}}
            }
        {{/if}}
        {{/each}}
    },
{{#if @last}}
)
{{/if}}
{{/each}}
{{/unless}}
{{#each responses}}
{{#each responseHeaders}}
{{#with schema}}
{{> model_templates/schema modelsHaveSchemaSuffix=schemaImports}}
{{/with}}
{{#unless isStub}}
{{paramName}}_parameter = api_client.HeaderParameter(
    name="{{baseName}}",
{{#if style}}
    style=api_client.ParameterStyle.{{style}},
{{/if}}
{{#if schema}}
{{#with schema}}
    schema={{baseName}},
{{/with}}
{{/if}}
{{#if required}}
    required=True,
{{/if}}
{{#if isExplode}}
    explode=True,
{{/if}}
)
{{/unless}}
{{/each}}
{{#each content}}
{{#with this.schema}}
{{> model_templates/schema modelsHaveSchemaSuffix=schemaImports}}
{{/with}}
{{/each}}
{{#if responseHeaders}}
ResponseHeadersFor{{code}} = typing_extensions.TypedDict(
    'ResponseHeadersFor{{code}}',
    {
{{#each responseHeaders}}
        '{{baseName}}': {{#with schema}}{{baseName}},{{/with}}
{{/each}}
    }
)
{{/if}}


{{> endpoint_response_for}}


{{> endpoint_response_for async=true}}


{{#if isDefault}}
_response_for_default = api_client.OpenApiResponse(
    response_cls=ApiResponseForDefault,
{{else}}
_response_for_{{code}} = api_client.OpenApiResponse(
    response_cls=ApiResponseFor{{code}},
    response_cls_async=ApiResponseFor{{code}}Async,
{{/if}}
{{#each content}}
{{#if @first}}
    content={
{{/if}}
        '{{{@key}}}': api_client.MediaType({{#if this.schema}}
            schema={{this.schema.baseName}}{{/if}}),
{{#if @last}}
    },
{{/if}}
{{/each}}
{{#if responseHeaders}}
    headers=[
{{#each responseHeaders}}
        {{paramName}}_parameter,
{{/each}}
    ]
{{/if}}
)
{{/each}}
{{#unless isStub}}
_status_code_to_response = {
{{#each responses}}
{{#if isDefault}}
    'default': _response_for_default,
{{else}}
    '{{code}}': _response_for_{{code}},
{{/if}}
{{/each}}
}
{{/unless}}
{{#each produces}}
{{#if @first}}
_all_accept_content_types = (
{{/if}}
    '{{{this.mediaType}}}',
{{#if @last}}
)
{{/if}}
{{/each}}


class BaseApi(api_client.Api):

    def _{{operationId}}_mapped_args(
    {{> endpoint_args isMappedArgs=true isOverload=false skipDeserialization="null" contentType="null" interface=true}}
        {{> endpoint_args_mapped_args_impl}}

    {{> endpoint_operation_impl async=true}}


    {{> endpoint_operation_impl}}


class {{operationIdCamelCase}}{{#prstv2}}Raw{{/prstv2}}(BaseApi):
    # this class is used by api classes that refer to endpoints with operationId fn names

    {{> endpoint_interface_impl methodName=operationId}}
{{#prstv2}}


class {{operationIdCamelCase}}(BaseApi):

    {{> endpoint_pydantic_impl}}
{{/prstv2}}


class ApiFor{{httpMethod}}(BaseApi):
    # this class is used by api classes that refer to endpoints by path and http method names

    {{> endpoint_interface_impl methodName=httpMethod}}


{{/with}}
